/**
 * Definition for singly-linked list.
*/
 public class ListNode {
     public var val: Int
     public var next: ListNode?
     public init(_ val: Int) {
         self.val = val
         self.next = nil
     }
 }

class Solution {
    func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        if l1 == nil && l2 == nil else {
            return nil
        }
        
        let val = (l1?.val ?? 0) + (l2?.val ?? 0)
        var next1 = l1?.next
        let next2 = l2?.next
        
        if val >= 10 {
            if let n1 = next1 {
                n1.val += 1
            } else {
                next1 = ListNode(1)
            }
        }
        
        let node = ListNode(val % 10)
        node.next = addTwoNumbers(next1, next2)
        
        return node
    }
}
